options ps=MAX ls=MAX nodate nonumber nocenter;
*options nonotes nosource nosource2 errors=0;
*ods listing;
*proc printto print="C:\Users\BANQUE0_O_GODECHO\Documents\res\segreg\out.txt";
*run;

* CHANGING THE WORK DIRECTORY; 
libname user "C:\Users\Public\Documents\Sas_work_dir";

*libname me "C:\Users\BANQUE0_O_GODECHO\Documents\bdsas";
libname rec "C:\Users\Public\Documents\Results\Segreg\";
libname est_comp "C:\Users\Public\Documents\Results\Establishment_Composition\";


*libname rec "C:\Users\Public\Documents\Test_data";
libname test "C:\Users\Public\Documents\Test_data";
libname psid "C:\Users\Public\Documents\pseudo_id";
libname psid_fbo "C:\Users\Public\Documents\pseudo_id\pseudo_id_foreign_born";



*libname test "P:\Test_data";
libname po1994 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1994";
libname po1995 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1995";
libname po1996 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1996";
libname po1997 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1997";
libname po1998 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1998";
libname po1999 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_1999";
libname po2000 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2000";
libname po2001 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2001";
libname po2002 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2002";
libname po2003 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2003";
libname po2004 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2004";
libname po2005 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2005";
libname po2006 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2006";
libname po2007 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2007";
libname po2008 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2008";
libname po2009 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2009";
libname po2010 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2010";
libname po2011 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2011";
libname po2012 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2012";
libname po2013 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2013";
libname po2014 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2014\R�gions";
libname po2015 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2015\R�gions";
libname po2016 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2016\R�gions";
libname po2017 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2017\R�gions";
libname po2018 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2018";
libname po2019 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2019";
libname po2020 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2020";


libname pi2012 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2012\Ile de France";
libname pi2013 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2013\Ile de France";
libname pi2014 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2014\D�partements";
libname pi2015 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2015\D�partements";
libname pi2016 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2016\D�partements";
libname pi2017 "\\casd.fr\casdfs\Projets\INEPROG\Data\DADS_DADS Postes_2017\D�partements";


*BE: Mining, Manufacturing and Energy;
* F: Construction;
* A&G: A:Agriculture & G:Trade;
* H: Transportation;
* I: Accomodation;
* J & : J. Info. & Comm & R: Leisure;
* K: Finance;
* LM: Real Estate and professional services;
* N: Support services;
* OPQ: State, teaching and health; 
* STU: Other;

proc format; 

	value $ZEAT
	 "75","77","78","91"-"95"="1.IledeFrance"
	"08","10","51","52","02","60","80","27","76","18","28","36","37","41","45","14","50","61","21","58","71","89"="2.BassinParisien"
	"59","62"="3.Nord"
	"54","55","57","88","67","68","25","39","70","90"="4.Est"
	"44","49","53","72","85","22","29","35","56","16","17","79","86"="5.Ouest"
	"24","33","40","47","64","09","12","31","32","46","65","81","82","19","23","87"="7.SudOuest"
	"01","07","26","38","42","69","73","74","03","15","43","63"="8.CentreEst"
	"11","30","34","48","66","04","05","06","13","83","84","20","2A","2B","2a","2b"="9.M�diterrrann�e";



	value $ind08_sectagg
		"05"-"39"="BE"
		"41"-"43"="F"
		"01"-"03","45"-"47"="A&G"
		"49"-"53"="H"
		"55"-"56"="I"
		"58"-"63","90"-"93"="J&R"
		"64"-"66"="K"
		"68"-"75"="LM"
		"77"-"82"="N"
		"84"-"88"="OPQ"
		"","94"-"99"="STU"
		OTHER="STU";

	value $naf03_naf08_
	"011A"="0111Z"
	"011C"="0111Z"
	"011D"="0113Z"
	"011F"="0122Z"
	"011G"="0121Z"
	"012A"="0141Z"
	"012C"="0143Z"
	"012E"="0146Z"
	"012G"="0147Z"
	"012J"="0144Z"
	"013Z"="0150Z"
	"014A"="0161Z"
	"014B"="8130Z"
	"014D"="0162Z"
	"015Z"="0170Z"
	"020A"="0129Z"
	"020B"="0220Z"
	"020D"="0240Z"
	"050A"="0311Z"
	"050C"="0321Z"
	"101Z"="0510Z"
	"102Z"="0520Z"
	"103Z"="0892Z"
	"111Z"="0610Z"
	"112Z"="0910Z"
	"120Z"="0721Z"
	"131Z"="0710Z"
	"132Z"="0729Z"
	"141A"="0811Z"
	"141C"="0811Z"
	"141E"="0811Z"
	"142A"="0812Z"
	"142C"="0812Z"
	"143Z"="0891Z"
	"144Z"="0893Z"
	"145Z"="0899Z"
	"151A"="0149Z"
	"151C"="1011Z"
	"151E"="1013A"
	"151F"="1013B"
	"152Z"="1020Z"
	"153A"="1031Z"
	"153C"="1032Z"
	"153E"="1039A"
	"153F"="1039B"
	"154A"="1041A"
	"154C"="1041B"
	"154E"="1042Z"
	"155A"="1051A"
	"155B"="1051B"
	"155C"="1051C"
	"155D"="1051D"
	"155F"="1052Z"
	"156A"="1061A"
	"156B"="1061B"
	"156D"="1062Z"
	"157A"="1091Z"
	"157C"="1092Z"
	"158A"="1071A"
	"158B"="1071B"
	"158C"="1071C"
	"158D"="1071D"
	"158F"="1072Z"
	"158H"="1081Z"
	"158K"="1082Z"
	"158M"="1073Z"
	"158P"="1083Z"
	"158R"="1084Z"
	"158T"="1086Z"
	"158V"="1085Z"
	"159A"="1101Z"
	"159B"="1101Z"
	"159D"="1101Z"
	"159F"="1102A"
	"159G"="1102B"
	"159J"="1103Z"
	"159L"="1104Z"
	"159N"="1105Z"
	"159Q"="1106Z"
	"159S"="1107A"
	"159T"="1107B"
	"160Z"="1200Z"
	"171A"="1310Z"
	"171C"="1310Z"
	"171E"="1310Z"
	"171F"="1310Z"
	"171H"="1310Z"
	"171K"="1310Z"
	"171M"="1310Z"
	"171P"="1310Z"
	"172A"="1320Z"
	"172C"="1320Z"
	"172E"="1320Z"
	"172G"="1320Z"
	"172J"="1320Z"
	"173Z"="1330Z"
	"174A"="1392Z"
	"174B"="1392Z"
	"174C"="1392Z"
	"175A"="1393Z"
	"175C"="1394Z"
	"175E"="1395Z"
	"175G"="1396Z"
	"176Z"="1391Z"
	"177A"="1419Z"
	"177C"="1439Z"
	"181Z"="1411Z"
	"182A"="1412Z"
	"182C"="1413Z"
	"182D"="1413Z"
	"182E"="1413Z"
	"182G"="1414Z"
	"182J"="1419Z"
	"183Z"="1320Z"
	"191Z"="1511Z"
	"192Z"="1512Z"
	"193Z"="1520Z"
	"201A"="1610A"
	"201B"="1610B"
	"202Z"="1621Z"
	"203Z"="1622Z"
	"204Z"="1624Z"
	"205A"="1629Z"
	"205C"="1629Z"
	"211A"="1711Z"
	"211C"="1712Z"
	"212A"="1721A"
	"212B"="1721B"
	"212C"="1721C"
	"212E"="1722Z"
	"212G"="1723Z"
	"212J"="1724Z"
	"212L"="1729Z"
	"221A"="3299Z"
	"221C"="5813Z"
	"221E"="5814Z"
	"221G"="5920Z"
	"221J"="5819Z"
	"222A"="1811Z"
	"222C"="1723Z"
	"222E"="1814Z"
	"222G"="1813Z"
	"222J"="1813Z"
	"223A"="1820Z"
	"223C"="1820Z"
	"223E"="1820Z"
	"231Z"="1910Z"
	"232Z"="1920Z"
	"233Z"="2013A"
	"241A"="2011Z"
	"241C"="2012Z"
	"241E"="2013B"
	"241G"="1910Z"
	"241J"="0891Z"
	"241L"="2016Z"
	"241N"="2017Z"
	"242Z"="2020Z"
	"243Z"="2030Z"
	"244A"="2110Z"
	"244C"="2120Z"
	"244D"="2120Z"
	"245A"="2041Z"
	"245C"="2042Z"
	"246A"="2051Z"
	"246C"="2052Z"
	"246E"="2053Z"
	"246G"="2059Z"
	"246J"="2680Z"
	"246L"="2059Z"
	"247Z"="2060Z"
	"251A"="2211Z"
	"251C"="2211Z"
	"251E"="2219Z"
	"252A"="2221Z"
	"252C"="2222Z"
	"252E"="2223Z"
	"252G"="2229A"
	"252H"="2229A"
	"261A"="2311Z"
	"261C"="2312Z"
	"261E"="2313Z"
	"261G"="2314Z"
	"261J"="2319Z"
	"261K"="2319Z"
	"262A"="2341Z"
	"262C"="2342Z"
	"262E"="2343Z"
	"262G"="2344Z"
	"262J"="2349Z"
	"262L"="2320Z"
	"263Z"="2331Z"
	"264A"="2332Z"
	"264B"="2332Z"
	"264C"="2332Z"
	"265A"="2351Z"
	"265C"="2352Z"
	"265E"="2352Z"
	"266A"="2361Z"
	"266C"="2362Z"
	"266E"="2363Z"
	"266G"="2364Z"
	"266J"="2365Z"
	"266L"="2369Z"
	"267Z"="2370Z"
	"268A"="2391Z"
	"268C"="2399Z"
	"271Y"="2410Z"
	"272A"="2451Z"
	"272C"="2420Z"
	"273A"="2431Z"
	"273C"="2432Z"
	"273E"="2433Z"
	"273G"="2434Z"
	"274A"="2441Z"
	"274C"="2442Z"
	"274D"="2442Z"
	"274F"="2443Z"
	"274G"="2443Z"
	"274J"="2444Z"
	"274K"="2444Z"
	"274M"="2445Z"
	"275A"="2451Z"
	"275C"="2452Z"
	"275E"="2453Z"
	"275G"="2454Z"
	"281A"="2433Z"
	"281C"="2512Z"
	"282C"="2529Z"
	"282D"="2521Z"
	"283A"="2530Z"
	"283B"="2530Z"
	"283C"="3311Z"
	"284A"="2550A"
	"284B"="2550B"
	"284C"="2550A"
	"285A"="2561Z"
	"285C"="2562A"
	"285D"="2562B"
	"286A"="2571Z"
	"286C"="2573B"
	"286D"="2573B"
	"286F"="2572Z"
	"287A"="2591Z"
	"287C"="2592Z"
	"287E"="2593Z"
	"287G"="2594Z"
	"287H"="2593Z"
	"287J"="2593Z"
	"287L"="2599A"
	"287N"="2599B"
	"287Q"="2571Z"
	"291A"="2811Z"
	"291B"="2812Z"
	"291D"="2812Z"
	"291E"="2813Z"
	"291F"="2812Z"
	"291H"="2815Z"
	"291J"="2815Z"
	"292A"="2821Z"
	"292C"="2822Z"
	"292D"="2822Z"
	"292F"="2825Z"
	"292H"="2829A"
	"292J"="2829A"
	"292L"="2829B"
	"292M"="2829B"
	"293A"="2830Z"
	"293C"="3312Z"
	"293D"="2830Z"
	"294A"="2841Z"
	"294B"="2849Z"
	"294C"="2824Z"
	"294D"="2790Z"
	"294E"="2849Z"
	"295A"="2891Z"
	"295B"="2892Z"
	"295D"="2892Z"
	"295E"="2893Z"
	"295G"="2894Z"
	"295J"="2895Z"
	"295L"="2894Z"
	"295M"="2896Z"
	"295N"="2573A"
	"295Q"="2899B"
	"295R"="2894Z"
	"296A"="2540Z"
	"296B"="2540Z"
	"297A"="2751Z"
	"297C"="2752Z"
	"300A"="2620Z"
	"300C"="2620Z"
	"311A"="2611Z"
	"311B"="2611Z"
	"311C"="3313Z"
	"312A"="2611Z"
	"312B"="2611Z"
	"313Z"="2611Z"
	"314Z"="2720Z"
	"315A"="2740Z"
	"315B"="2740Z"
	"315C"="2740Z"
	"316A"="2740Z"
	"316C"="2599B"
	"316D"="2344Z"
	"321A"="2611Z"
	"321C"="2611Z"
	"321D"="2612Z"
	"322A"="2630Z"
	"322B"="2630Z"
	"323Z"="2620Z"
	"331A"="2660Z"
	"331B"="2660Z"
	"332A"="2651A"
	"332B"="2651B"
	"333Z"="3320C"
	"334A"="3250B"
	"334B"="2670Z"
	"335Z"="2652Z"
	"341Z"="2892Z"
	"342A"="2920Z"
	"342B"="2920Z"
	"343Z"="2811Z"
	"351A"="3011Z"
	"351B"="3011Z"
	"351C"="3011Z"
	"351E"="3012Z"
	"352Z"="3020Z"
	"353A"="2899B"
	"353B"="3030Z"
	"353C"="3030Z"
	"354A"="3091Z"
	"354C"="3092Z"
	"354E"="3092Z"
	"355Z"="2822Z"
	"361A"="2932Z"
	"361C"="2823Z"
	"361E"="3102Z"
	"361G"="3109B"
	"361H"="3109B"
	"361J"="3109B"
	"361K"="3109B"
	"361M"="3103Z"
	"362A"="3211Z"
	"362C"="3212Z"
	"363Z"="3220Z"
	"364Z"="3230Z"
	"365Z"="2640Z"
	"366A"="3213Z"
	"366C"="3291Z"
	"366E"="1399Z"
	"371Z"="3831Z"
	"372Z"="3831Z"
	"401A"="3511Z"
	"401C"="3512Z"
	"401E"="3513Z"
	"402A"="3521Z"
	"402C"="3522Z"
	"403Z"="3530Z"
	"410Z"="3600Z"
	"451A"="4311Z"
	"451B"="4312B"
	"451D"="4313Z"
	"452A"="4120A"
	"452B"="4120B"
	"452C"="4120B"
	"452D"="4212Z"
	"452E"="4221Z"
	"452F"="4222Z"
	"452J"="4391B"
	"452K"="4399A"
	"452L"="4391A"
	"452N"="4212Z"
	"452P"="4120B"
	"452R"="4221Z"
	"452T"="4399B"
	"452U"="3900Z"
	"452V"="4399C"
	"453A"="4321A"
	"453C"="4329A"
	"453E"="4322A"
	"453F"="4322B"
	"453H"="4321B"
	"454A"="4331Z"
	"454C"="4332A"
	"454D"="4332B"
	"454F"="4333Z"
	"454H"="4334Z"
	"454J"="4334Z"
	"454L"="4332C"
	"454M"="4339Z"
	"455Z"="4399E"
	"501Z"="4511Z"
	"502Z"="4520A"
	"503A"="4531Z"
	"503B"="4532Z"
	"504Z"="4540Z"
	"505Z"="4730Z"
	"511A"="4611Z"
	"511C"="4612B"
	"511E"="4613Z"
	"511G"="4614Z"
	"511J"="4615Z"
	"511L"="4616Z"
	"511N"="4617B"
	"511P"="4617A"
	"511R"="4618Z"
	"511T"="4619B"
	"511U"="4612A"
	"512A"="4621Z"
	"512C"="4622Z"
	"512E"="4623Z"
	"512G"="4624Z"
	"512J"="4621Z"
	"513A"="1039A"
	"513C"="4632A"
	"513D"="4632B"
	"513E"="4632C"
	"513G"="4633Z"
	"513J"="1101Z"
	"513L"="4635Z"
	"513N"="4636Z"
	"513Q"="4637Z"
	"513S"="4638A"
	"513T"="4638B"
	"513V"="4639A"
	"513W"="4639B"
	"514A"="4641Z"
	"514C"="4642Z"
	"514D"="4642Z"
	"514F"="4643Z"
	"514H"="4644Z"
	"514J"="4644Z"
	"514L"="4645Z"
	"514N"="4646Z"
	"514Q"="4649Z"
	"514R"="4649Z"
	"514S"="4643Z"
	"515A"="4671Z"
	"515C"="4672Z"
	"515E"="4673A"
	"515F"="4673A"
	"515H"="4674A"
	"515J"="4674B"
	"515L"="4675Z"
	"515N"="4676Z"
	"515Q"="4677Z"
	"518A"="4662Z"
	"518C"="4663Z"
	"518E"="4664Z"
	"518G"="4651Z"
	"518H"="4665Z"
	"518J"="4652Z"
	"518L"="4669A"
	"518M"="4669B"
	"518N"="4669C"
	"518P"="4661Z"
	"519A"="4690Z"
	"519B"="4690Z"
	"521A"="4711A"
	"521B"="4711B"
	"521C"="4711C"
	"521D"="4711D"
	"521E"="4711E"
	"521F"="4711F"
	"521H"="4719A"
	"521J"="4719B"
	"522A"="4721Z"
	"522C"="4722Z"
	"522E"="4723Z"
	"522G"="4724Z"
	"522J"="4725Z"
	"522L"="4726Z"
	"522N"="4729Z"
	"522P"="4721Z"
	"523A"="4773Z"
	"523C"="4774Z"
	"523E"="4775Z"
	"524A"="4751Z"
	"524C"="4771Z"
	"524E"="4772A"
	"524F"="4772B"
	"524H"="4759A"
	"524J"="4753Z"
	"524L"="4743Z"
	"524N"="4752A"
	"524P"="4752B"
	"524R"="4761Z"
	"524T"="4778A"
	"524U"="4753Z"
	"524V"="4777Z"
	"524W"="4764Z"
	"524X"="4776Z"
	"524Y"="4778B"
	"524Z"="4741Z"
	"525Z"="4779Z"
	"526A"="4791A"
	"526B"="4791B"
	"526D"="4781Z"
	"526E"="4782Z"
	"526G"="4799A"
	"526H"="4799B"
	"527A"="9523Z"
	"527C"="9521Z"
	"527D"="9522Z"
	"527F"="9525Z"
	"527H"="9512Z"
	"551A"="5510Z"
	"551C"="5510Z"
	"551E"="5510Z"
	"552A"="5520Z"
	"552C"="5530Z"
	"552E"="5510Z"
	"552F"="5590Z"
	"553A"="5610A"
	"553B"="5610C"
	"554A"="5630Z"
	"554B"="5630Z"
	"554C"="5630Z"
	"555A"="5629B"
	"555C"="5629A"
	"555D"="5621Z"
	"601Z"="4910Z"
	"602A"="4931Z"
	"602B"="4939A"
	"602C"="4931Z"
	"602E"="4932Z"
	"602G"="4939B"
	"602L"="4941B"
	"602M"="4941A"
	"602N"="4942Z"
	"602P"="4941C"
	"603Z"="4950Z"
	"611A"="5010Z"
	"611B"="5010Z"
	"612Z"="5030Z"
	"621Z"="5110Z"
	"622Z"="5110Z"
	"623Z"="5122Z"
	"631A"="5224A"
	"631B"="5224B"
	"631D"="5210A"
	"631E"="5210B"
	"632A"="4932Z"
	"632C"="5222Z"
	"632E"="5223Z"
	"633Z"="7911Z"
	"634A"="5229A"
	"634B"="5229B"
	"634C"="5229B"
	"641A"="5310Z"
	"641C"="5320Z"
	"642C"="6110Z"
	"642D"="6110Z"
	"651A"="6411Z"
	"651C"="6419Z"
	"651D"="6419Z"
	"651E"="6419Z"
	"651F"="6419Z"
	"652A"="6491Z"
	"652C"="6492Z"
	"652E"="6420Z"
	"652F"="6499Z"
	"660A"="6511Z"
	"660C"="6520Z"
	"660E"="6512Z"
	"660F"="6520Z"
	"660G"="6512Z"
	"671A"="6611Z"
	"671C"="6612Z"
	"671E"="6612Z"
	"672Z"="6621Z"
	"701A"="4110A"
	"701B"="4110B"
	"701C"="4110C"
	"701D"="4110D"
	"701F"="6810Z"
	"702A"="6820A"
	"702B"="6820B"
	"702C"="6820B"
	"703A"="6831Z"
	"703C"="6832A"
	"703D"="6832A"
	"703E"="6619A"
	"711A"="7711A"
	"711B"="7711B"
	"712A"="7712Z"
	"712C"="7734Z"
	"712E"="7735Z"
	"713A"="7731Z"
	"713C"="7732Z"
	"713E"="7733Z"
	"713G"="7739Z"
	"714A"="7729Z"
	"714B"="7721Z"
	"721Z"="6202A"
	"722A"="5821Z"
	"722C"="6201Z"
	"723Z"="6203Z"
	"724Z"="5811Z"
	"725Z"="3312Z"
	"726Z"="6209Z"
	"731Z"="7211Z"
	"732Z"="7220Z"
	"741A"="6910Z"
	"741C"="6920Z"
	"741E"="7320Z"
	"741G"="0240Z"
	"741J"="6420Z"
	"742A"="7111Z"
	"742B"="7112A"
	"742C"="7112B"
	"743A"="7120A"
	"743B"="7120B"
	"744A"="7311Z"
	"744B"="7311Z"
	"745A"="7810Z"
	"745B"="7820Z"
	"746Z"="7490B"
	"747Z"="8121Z"
	"748A"="7420Z"
	"748B"="7420Z"
	"748D"="8292Z"
	"748F"="7430Z"
	"748G"="8219Z"
	"748H"="8220Z"
	"748J"="8230Z"
	"748K"="4779Z"
	"751A"="8411Z"
	"751C"="8412Z"
	"751E"="8413Z"
	"751G"="8110Z"
	"752A"="8421Z"
	"752C"="8422Z"
	"752E"="8423Z"
	"752G"="8424Z"
	"752J"="8425Z"
	"753A"="8430A"
	"753B"="8430B"
	"753C"="8430C"
	"801Z"="8510Z"
	"802A"="8531Z"
	"802C"="8532Z"
	"803Z"="8541Z"
	"804A"="8532Z"
	"804C"="8532Z"
	"804D"="8552Z"
	"851A"="8610Z"
	"851C"="8621Z"
	"851E"="8623Z"
	"851G"="8690D"
	"851H"="8690E"
	"851J"="8690A"
	"851K"="8690B"
	"851L"="8690C"
	"852Z"="7500Z"
	"853A"="8710B"
	"853B"="8710B"
	"853C"="8710C"
	"853D"="8710A"
	"853E"="8720A"
	"853G"="8891A"
	"853H"="8810B"
	"853J"="8810A"
	"853K"="8899A"
	"900A"="3700Z"
	"900B"="3811Z"
	"900E"="3812Z"
	"900G"="3811Z"
	"911A"="9411Z"
	"911C"="9412Z"
	"912Z"="9420Z"
	"913A"="9491Z"
	"913C"="9492Z"
	"913E"="9499Z"
	"921A"="5911A"
	"921B"="5911B"
	"921C"="5911C"
	"921D"="5911C"
	"921F"="5913A"
	"921G"="5913B"
	"921J"="5914Z"
	"922A"="5920Z"
	"922B"="5911A"
	"922D"="6020A"
	"922E"="6020B"
	"922F"="6110Z"
	"923A"="9001Z"
	"923B"="9002Z"
	"923D"="7990Z"
	"923F"="9321Z"
	"923K"="7990Z"
	"924Z"="6391Z"
	"925A"="9101Z"
	"925C"="9102Z"
	"925E"="9104Z"
	"926A"="9311Z"
	"926C"="7990Z"
	"927A"="9200Z"
	"927C"="0162Z"
	"930A"="9601A"
	"930B"="9601B"
	"930D"="9602A"
	"930E"="9602B"
	"930G"="9603Z"
	"930H"="9603Z"
	"930K"="9604Z"
	"930L"="9313Z"
	"930N"="8551Z"
	"950Z"="9700Z"
	"960Z"="9810Z"
	"970Z"="9820Z"
	"990Z"="9900Z"
	"0320"="0321Z"
"2202"="2830Z"
"4107"="1103Z"
"5510"="8130Z"
"5560"="4399C"
"5571"="4332B"
"5701"="4611Z"
"6007"="4611Z"
"6211"="4711B"
"6241"="4721Z"
"6411"="4771Z"
"6414"="4751Z"
"6425"="4719B"
"6502"="4730Z"
"6702"="5629A"
"7708"="6910Z"
"7712"="4339Z"
"7713"="7820Z"
"7715"="9412Z"
"8111"="6820A"
"8121"="6820B"
"8206"="8553Z"
"8410"="8621Z"
"8503"="8710C"
"8606"="9001Z"
"8608"="9002Z"
"8610"="8531Z"
"8804"="6512Z"
"9011"="8411Z"
"9012"="8413Z"
"9212"="8520Z"
"9214"="8531Z"
"9616"="9329Z"
"9625"="9312Z"
"9722"="9491Z"
"9723"="9499Z"
"271Z"="2410Z"
"273J"="2445Z"
"282A"="2529Z"
"282B"="2529Z"
"287M"="2572Z"
"287P"="2599B"
"291C"="2813Z"
"292K"="2829B"
"295C"="2892Z"
"295P"="2899B"
"321B"="2611Z"
"401Z"="2511Z"
"402Z"="3521Z"
"516A"="4662Z"
"516C"="4663Z"
"516E"="4664Z"
"516G"="4651Z"
"516J"="4669A"
"516K"="4669B"
"516L"="4669C"
"516N"="4661Z"
"517Z"="4690Z"
"551D"="5590Z"
"642A"="6110Z"
"642B"="6190Z"
"711Z"="7711A"
"722Z"="5829C"
"900C"="3822Z"
"922C"="6020A"
"923H"="9329Z"
"923J"="9001Z"
	;

	value $div03_sect03_
	"01"="A"
	"02"="A"
	"05"="B"
	"10"="C"
	"11"="C"
	"12"="C"
	"13"="C"
	"14"="C"
	"15"="D"
	"16"="D"
	"17"="D"
	"18"="D"
	"19"="D"
	"20"="D"
	"21"="D"
	"22"="D"
	"23"="D"
	"24"="D"
	"25"="D"
	"26"="D"
	"27"="D"
	"28"="D"
	"29"="D"
	"30"="D"
	"31"="D"
	"32"="D"
	"33"="D"
	"34"="D"
	"35"="D"
	"36"="D"
	"37"="D"
	"40"="E"
	"41"="E"
	"45"="F"
	"50"="G"
	"51"="G"
	"52"="G"
	"55"="H"
	"60"="I"
	"61"="I"
	"62"="I"
	"63"="I"
	"64"="I"
	"65"="J"
	"66"="J"
	"67"="J"
	"70"="K"
	"71"="K"
	"72"="K"
	"73"="K"
	"74"="K"
	"75"="L"
	"80"="M"
	"85"="N"
	"90"="O"
	"91"="O"
	"92"="O"
	"93"="O"
	"95"="P"
	"96"="P"
	"97"="P"
	"99"="Q"
	;

	value $sect03_ag
	"A"-"D"="AD"
	"E"-"F"="EF"
	"G"-"H"="GH"
	"I"="I"
	"J"="J"
	"K"="K"
	"L"-"M"="LM"
	"N"="N"
	"O"-"Q"="OPQ"
	OTHER="OPQ";
	;

	VALUE rk
	0.00-0.25="1.F0025"
	0.25-0.75="2.F2575"
	0.75-0.90="3.F7590"
	0.90-0.99="4.F9099" 
	0.99-1.00="5.F9910";

	VALUE decile
	0.0-0.1="d0"
	0.1-0.2="d1"
	0.2-0.3="d2"
	0.3-0.4="d3"
	0.4-0.5="d4"
	0.5-0.6="d5"
	0.6-0.7="d6"
	0.7-0.8="d7"
	0.8-0.9="d8"
	0.9-1.0="d9";

	VALUE age
	00-30="age15-30"
	31-40="age31-40"
	41-55="age41-55"
	56-199="age>=56" ;

	value $orgsizeb
	"00","01","02","03"="size_0-19"
	"04"="size_20-49"
	"05"="size_50-99"
	"06"="size_100-199"
	"07"="size_200-499" 
	"08"="size_500-999" 
	"09","10","11"="size_>1000" 
	;

	value orgsize
	0="orgsize0"
	1="orgsize1"
	2-20="orgsize2"
	21-50="orgsize20"
	51-100="orgsize50"
	101-200="orgsize100"
	201-500="orgsize200"
	501-1000="orgsize500" 
	1001-2500="orgsize1000" 
	2501-5000="orgsize2500" 
	5001-10000="orgsize5000" 
	10001-10000000="orgsize10000_plus"
	;
run;

%macro seg2(b);

	%if &b=b1994_1 %then %let year=1993.1;
	%else %if &b=b2002_1 %then %let year=2001.1;
	%else %if &b=b2009_1 %then %let year=2008.1;
	%else %let year=%substr(&b,2,4);

	%if &b=b1994_1 %then %let psyear=1994;
	%else %let psyear=&year;

	* CORRECTION OF MIGRANT STATUS FROM FULL PANEL;
	proc sql; 
	 
		create table &b.1  
			as select * from &b 
			as aa
			left join psid.psid_&psyear 
			as bb
			on aa.ident_s=bb.ident_s;

		create table &b.2  (rename=(fborn_def=migrant) 
							drop=minyear maxyear)
					as select * from &b.1 (rename=(migrant=migrant_old 
												   nonmigrant=nonmigrant_old )) 
							as aa
					left join psid_fbo.psid_all_foreign_born 
								(keep=ident_all fborn_def minyear maxyear 
								where=(minyear<=&psyear and maxyear>=&psyear)) 
							as bb
					on aa.ident_all=bb.ident_all; 

	quit;

	data &b.3; set &b.2;
			if migrant=. then migrant=migrant_old;
			/*if noncitizen=. then noncitizen=noncitizen_old;
			if domtom=. then domtom=domtom_old;*/
			
			nonmigrant=1-migrant;
			/*citizen=1-noncitizen;
			nondomtom=1-domtom;

			nonEU=(noncitizen=1 and EU_nat_origin NE 1)*1;
			EU=1-noncitizen-nonEU;*/

		run;

		proc datasets LIBRARY=USER; 
			DELETE &b &b.1 &b.2;
		QUIT;

		PROC DATASETS LIBRARY=USER; 
			CHANGE &b.3=&b;
		QUIT;

		%macro analysis(unit);
			proc sql; 
			create table &b.1 as select *, 
			SUM ((included=1)) as &unit._nb,
			SUM (weight*(included=1)) as &unit._weight
			from &b group by &unit.;
			quit;

			proc means data=&b.1; 
			Title "&year. &unit Selection Descriptives";
			var wage;
			class included &unit._weight ;
			types included &unit._weight included*&unit._weight;
			format &unit._weight orgsize.;
			output out=rec.&b.&unit.des;
			weight weight;
			run;

			data rec.&b.&unit.des; set rec.&b.&unit.des;
				year=&year.;
			run;

			proc sort data=&b.1 ; 
				by wage
				random; 
			run;

			data  &b.2; set &b.1 (where=(included=1 and &unit._nb>1 and missing(&unit)=0 )) 
									end=last; 
				sumweight + weight ;
				if last then call symput ('SUM', trim(left(put(sumweight, best.))));
			run;

			proc datasets LIBRARY=user; 
				DELETE &b.1;
			RUN;

			proc sql; 
				create table &b.3 as select *, 
				lnwage-MEAN(lnwage) as within_lnwage,
				sum((weight)) as countuny,
				sum(weight*(PUT((sumweight/&SUM),rk.)="1.F0025")) as F0025uny,
				sum(weight*(PUT((sumweight/&SUM),rk.)="2.F2575")) as F2575uny, 
				sum(weight*(PUT((sumweight/&SUM),rk.)="3.F7590")) as F7590uny, 
				sum(weight*(PUT((sumweight/&SUM),rk.)="4.F9099")) as F9099uny, 
				sum(weight*(PUT((sumweight/&SUM),rk.)="5.F9910")) as F9910uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d0")) as d0uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d1")) as d1uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d2")) as d2uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d3")) as d3uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d4")) as d4uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d5")) as d5uny,
				sum(weight*(PUT((sumweight/&SUM),decile.)="d6")) as d6uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d7")) as d7uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d8")) as d8uny, 
				sum(weight*(PUT((sumweight/&SUM),decile.)="d9")) as d9uny,
				sum(weight*(female=1)) as femaleuny,
				sum(weight*(female=0)) as maleuny, 
				sum(weight*(occ1=1)) as occ1uny, 
				sum(weight*(occ2=1)) as occ2uny, 
				sum(weight*(occ3=1)) as occ3uny,
				sum(weight*(migrant=1)) as migrantuny,
				sum(weight*(nonmigrant=1)) as nonmigrantuny,
				/*sum(weight*(educ1=1)) as educ1uny,
				sum(weight*(educ2=1)) as educ2uny,
				sum(weight*(educ3=1)) as educ3uny,
				sum(weight*(educ4=1)) as educ4uny,*/
				sum(weight*(age1=1)) as age1uny,
				sum(weight*(age2=1)) as age2uny,
				sum(weight*(age3=1)) as age3uny,
				sum(weight*(age4=1)) as age4uny
				from &b.2 (where=( wage NE .)) group by &unit.; 
			quit;

			
			%if &unit=est %then %do; 

				proc sql; 
					create table &b.est as select
					min(&unit.) as &unit.,
					min(&year.) as year,
					min(wtown) as wtown,
					min(nuts1) as nuts1,
					min(nuts3) as nuts3,
					min(sector) as sector,
					min(sector_agg) as sector_agg,
					sum(weight) as count,
					sum(weight*(PUT((sumweight/&SUM),rk.)="1.F0025")) as F0025,
					sum(weight*(PUT((sumweight/&SUM),rk.)="2.F2575")) as F2575, 
					sum(weight*(PUT((sumweight/&SUM),rk.)="3.F7590")) as F7590, 
					sum(weight*(PUT((sumweight/&SUM),rk.)="4.F9099")) as F9099, 
					sum(weight*(PUT((sumweight/&SUM),rk.)="5.F9910")) as F9910,
					sum(weight*(female=1)) as female,
					sum(weight*(female=0)) as male, 
					sum(weight*(occ1=1)) as occ1, 
					sum(weight*(occ2=1)) as occ2, 
					sum(weight*(occ3=1)) as occ3,
					sum(weight*(migrant=1)) as migrant,
					sum(weight*(nonmigrant=1)) as nonmigrant,
					/*sum(weight*(educ1=1)) as educ1,
					sum(weight*(educ2=1)) as educ2,
					sum(weight*(educ3=1)) as educ3,
					sum(weight*(educ4=1)) as educ4,*/
					sum(weight*(age1=1)) as age1,
					sum(weight*(age2=1)) as age2,
					sum(weight*(age3=1)) as age3,
					sum(weight*(age4=1)) as age4,
					mean(wage) as mwage,
					max(wage) as maxwage,
					std(wage) as sdwage,
					mean(log(wage)) as mlwage,
					std(log(wage)) as sdlwage,
					min(wage*(1+log(weight*(PUT((sumweight/&SUM),rk.)="1.F0025")))) as minwF0025,
					min(wage*(1+log(weight*(PUT((sumweight/&SUM),rk.)="2.F2575")))) as minwF2575, 
					min(wage*(1+log(weight*(PUT((sumweight/&SUM),rk.)="3.F7590")))) as minwF7590, 
					min(wage*(1+log(weight*(PUT((sumweight/&SUM),rk.)="4.F9099")))) as minwF9099, 
					min(wage*(1+log(weight*(PUT((sumweight/&SUM),rk.)="5.F9910")))) as minwF9910 /*,
					sum(weight*(PUT((sumweight/&SUM),decile.)="d0")) as d0,
					sum(weight*(PUT((sumweight/&SUM),decile.)="d1")) as d1, 
					sum(weight*(PUT((sumweight/&SUM),decile.)="d2")) as d2, 
					sum(weight*(PUT((sumweight/&SUM),decile.)="d3")) as d3, 
					sum(weight*(PUT((sumweight/&SUM),decile.)="d4")) as d4,
					sum(weight*(PUT((sumweight/&SUM),decile.)="d5")) as d5,
					sum(weight*(PUT((sumweight/&SUM),decile.)="d6")) as d6, 
					sum(weight*(PUT((sumweight/&SUM),decile.)="d7")) as d7, 
					sum(weight*(PUT((sumweight/&SUM),decile.)="d8")) as d8, 
					sum(weight*(PUT((sumweight/&SUM),decile.)="d9")) as d9,
					mean(log(wage*weight*(PUT((sumweight/&SUM),rk.)="1.F0025"))) as mlwF0025,
					mean(log(wage*weight*(PUT((sumweight/&SUM),rk.)="2.F2575"))) as mlwF2575, 
					mean(log(wage*weight*(PUT((sumweight/&SUM),rk.)="3.F7590"))) as mlwF7590, 
					mean(log(wage*weight*(PUT((sumweight/&SUM),rk.)="4.F9099"))) as mlwF9099, 
					mean(log(wage*weight*(PUT((sumweight/&SUM),rk.)="5.F9910"))) as mlwF9910,
					mean(log(wage*weight*(female=1))) as mlwfemale,
					mean(log(wage*weight*(female=0))) as mlwmale, 
					mean(log(wage*weight*(occ1=1))) as mlwocc1, 
					mean(log(wage*weight*(occ2=1))) as mlwocc2, 
					mean(log(wage*weight*(occ3=1))) as mlwocc3,
					mean(log(wage*weight*(migrant=1))) as mlwmigrant,
					mean(log(wage*weight*(nonmigrant=1))) as mlwnonmigrant,		
					std(log(wage*weight*(PUT((sumweight/&SUM),rk.)="1.F0025"))) as sdlwF0025,
					std(log(wage*weight*(PUT((sumweight/&SUM),rk.)="2.F2575"))) as sdlwF2575, 
					std(log(wage*weight*(PUT((sumweight/&SUM),rk.)="3.F7590"))) as sdlwF7590, 
					std(log(wage*weight*(PUT((sumweight/&SUM),rk.)="4.F9099"))) as sdlwF9099, 
					std(log(wage*weight*(PUT((sumweight/&SUM),rk.)="5.F9910"))) as sdlwF9910,
					std(log(wage*weight*(female=1))) as sdlwfemale,
					std(log(wage*weight*(female=0))) as sdlwmale, 
					std(log(wage*weight*(occ1=1))) as sdlwocc1, 
					std(log(wage*weight*(occ2=1))) as sdlwocc2, 
					std(log(wage*weight*(occ3=1))) as sdlwocc3,
					std(log(wage*weight*(migrant=1))) as sdlwmigrant,
					std(log(wage*weight*(nonmigrant=1))) as sdlwnonmigrant*/
					from &b.2 (where=( wage NE .)) group by &unit.;

				 create table est_comp.&b.est (drop=minwF0025 minwF2575 minwF7590 minwF9099 minwF9910)
						as select *, 
						min (minwF0025) as  minawF0025,
						min (minwF2575) as  minawF2575,
						min (minwF7590) as  minawF7590,
						min (minwF9099) as  minawF9099,
						min (minwF9910) as  minawF9910
						from &b.est
						; 
				quit;

				proc datasets lib=user;
					delete &b.est;
				RUN;

			%end;
			
			proc datasets LIBRARY=user; 
				DELETE &b.2;
			RUN;

			data &b.4; set &b.3;
			rk=sumweight/&SUM;

			rename age_g=age occ_g=occ /*educ_g=educ*/;

			F0025=(PUT(rk,rk.)="1.F0025");
			F2575=(PUT(rk,rk.)="2.F2575");
			F7590=(PUT(rk,rk.)="3.F7590");
			F9099=(PUT(rk,rk.)="4.F9099");
			F9910=(PUT(rk,rk.)="5.F9910");

			d0=(PUT(rk,decile.)="d0");
			d1=(PUT(rk,decile.)="d1");
			d2=(PUT(rk,decile.)="d2");
			d3=(PUT(rk,decile.)="d3");
			d4=(PUT(rk,decile.)="d4");
			d5=(PUT(rk,decile.)="d5");
			d6=(PUT(rk,decile.)="d6");
			d7=(PUT(rk,decile.)="d7");
			d8=(PUT(rk,decile.)="d8");
			d9=(PUT(rk,decile.)="d9");

			if countuny>weight then do; 

				xF0025=(F0025uny-F0025*weight)/(countuny-weight);
				xF2575=(F2575uny-F2575*weight)/(countuny-weight);
				xF7590=(F7590uny-F7590*weight)/(countuny-weight);
				xF9099=(F9099uny-F9099*weight)/(countuny-weight);
				xF9910=(F9910uny-F9910*weight)/(countuny-weight);

				xd0=(d0uny-d0*weight)/(countuny-weight);
				xd1=(d1uny-d1*weight)/(countuny-weight);
				xd2=(d2uny-d2*weight)/(countuny-weight);
				xd3=(d3uny-d3*weight)/(countuny-weight);
				xd4=(d4uny-d4*weight)/(countuny-weight);
				xd5=(d5uny-d5*weight)/(countuny-weight);
				xd6=(d6uny-d6*weight)/(countuny-weight);
				xd7=(d7uny-d7*weight)/(countuny-weight);
				xd8=(d8uny-d8*weight)/(countuny-weight);
				xd9=(d9uny-d9*weight)/(countuny-weight);

				/*xeduc1=(educ1uny-educ1*weight)/(countuny-weight);
				xeduc2=(educ2uny-educ2*weight)/(countuny-weight);
				xeduc3=(educ3uny-educ3*weight)/(countuny-weight);
				xeduc4=(educ4uny-educ4*weight)/(countuny-weight);*/

				xocc1=(occ1uny-occ1*weight)/(countuny-weight);
				xocc2=(occ2uny-occ2*weight)/(countuny-weight);
				xocc3=(occ3uny-occ3*weight)/(countuny-weight);

				xage1=(age1uny-age1*weight)/(countuny-weight);
				xage2=(age2uny-age2*weight)/(countuny-weight);
				xage3=(age3uny-age3*weight)/(countuny-weight);
				xage4=(age4uny-age4*weight)/(countuny-weight);

				xfemale=(femaleuny-female*weight)/(countuny-weight);
				xmale=(maleuny-weight+female*weight)/(countuny-weight);

				xmigrant=(migrantuny-migrant*weight)/(countuny-weight);
				xnonmigrant=(nonmigrantuny-weight+migrant*weight)/(countuny-weight);

			end;


			all=1;
			if countuny>0 then w_unit=1/countuny;
			run;

			proc datasets LIBRARY=user; 
				DELETE &b.3;
			RUN;

			proc means data= &b.4 ; 
			Title "&year. &unit Fractile Exposure per Fractile &SUM";
			var wage lnwage within_lnwage w_unit xf0025 xf2575 xf7590 xf9099 xf9910;

			%if &unit=est %then %do;
				class all rk /*orgsize*/ &unit._weight manufacturing fin_service financial_center sector_agg nuts1; 
				types all rk /*rk*orgsize*/ rk*&unit._weight rk*manufacturing rk*fin_service rk*financial_center rk*sector_agg rk*nuts1;
			%end;

			%else %if &unit=wtown %then %do;
				class all rk financial_center; 
				types all rk rk*financial_center;
			%end;

			%else %do;
			class all rk ; 
			format rk rk.;
			%end;

			format rk rk. &unit._weight orgsize. /* orgsize $orgsizeb.*/;
			output out=rec.&b.out&unit.inc;
			weight weight;
			where weight>0;
			run;

			data rec.&b.out&unit.inc; set rec.&b.out&unit.inc; 
			year=&year.;
			run;

			proc means data= &b.4 ; 
			Title "&year. &unit Decile Exposure per Fractile &SUM";
			var wage w_unit xd0-xd9;
			class all rk; 
			types all rk; 
			format rk decile. ;
			output out=rec.&b.out&unit.dinc;
			weight weight;
			where weight>0;
			run;

			data rec.&b.out&unit.dinc; set rec.&b.out&unit.dinc; 
			year=&year.;
			run;

			/*
			proc means data= &b.4 ; 
			Title "&year. &unit Education Exposure per Education";
			var wage w_unit xeduc1 xeduc2 xeduc3 xeduc4 ;
			class all educ; 
			types all educ; 
			output out=rec.&b.out&unit.educ; 
			weight weight;
			run;

			data rec.&b.out&unit.educ; set rec.&b.out&unit.educ; 
			year=&year.;
			run;*/


			proc means data= &b.4 ; 
			Title "&year. &unit Occupation Exposure per Occupation";
			var wage w_unit xocc1 xocc2 xocc3;
			class all occ; 
			types all occ; 
			output out=rec.&b.out&unit.occ; 
			weight weight;
			where weight>0;
			run;

			data rec.&b.out&unit.occ; set rec.&b.out&unit.occ; 
			year=&year.;
			run;

			proc means data= &b.4 ; 
			Title "&year. &unit Age Exposure per Age";
			var wage w_unit xage1 xage2 xage3 xage4 ;
			class all age;
			types all age;
			output out=rec.&b.out&unit.age;
			weight weight;
			where weight>0;
			run;

			data rec.&b.out&unit.age; set rec.&b.out&unit.age; 
			year=&year.;
			run;

			proc means data= &b.4 ; 
			Title "&year. &unit Gender Exposure per Gender";
			var wage w_unit xfemale xmale ;
			class all female; 
			types all female; 
			output out=rec.&b.out&unit.Sex; 
			weight weight;
			where weight>0;
			run;

			data rec.&b.out&unit.Sex; set rec.&b.out&unit.Sex; 
			year=&year.;
			run;

			proc means data= &b.4 ; 
			Title "&year. &unit migrant Exposure per Foreign Status";
			var wage w_unit xmigrant xnonmigrant ;
			class all migrant; 
			types all migrant; 
			output out=rec.&b.out&unit.mig; 
			weight weight;
			where weight>0;
			run;

			data rec.&b.out&unit.mig; set rec.&b.out&unit.mig; 
			year=&year.;
			run;

			proc datasets lib=user memtype=data /*noprint*/;
				delete &b.4;
			RUN;

		%mend;

		%analysis(est);
		%analysis(firm);
		*%analysis(wtown);

		%if &year>1987 %then %analysis(ltown);
			proc datasets lib=user memtype=data /*noprint*/;
			delete &b &b.0 ;
			RUN;
%mend;


%macro common;
if siren NOT in ("","0","000000000","00000000");

if year<=2001 then siret=compress(""!!compress(substr(siren,2,9)!!substr(nic,2,5))!!"");
else siret=compress(""!!compress(siren!!nic)!!"");

weight=1;
myid=_N_;

*THRESHOLD DEFINITION THANKS TO MINIMUM WAGE ;
if round(year,1)=1976 then smic=8.94; 
else if round(year,1)=1977 then smic=10.06; 
else if round(year,1)=1978 then smic=11.31; 
else if round(year,1)=1979 then smic=12.93; 
else if round(year,1)=1980 then smic=14.79; 
else if round(year,1)=1980 then smic=14.79; 
else if round(year,1)=1981 then smic=17.76;
else if round(year,1)=1982 then smic=20.29;
else if round(year,1)=1983 then smic=22.33;
else if round(year,1)=1984 then smic=24.36;
else if round(year,1)=1985 then smic=26.04;
else if round(year,1)=1986 then smic=26.92;
else if round(year,1)=1987 then smic=27.84;
else if round(year,1)=1988 then smic=28.76;
else if round(year,1)=1989 then smic=29.91;
else if round(year,1)=1990 then smic=31.94;
else if round(year,1)=1991 then smic=32.66;
else if round(year,1)=1992 then smic=34.06;
else if round(year,1)=1993 then smic=34.83;
else if round(year,1)=1994 then smic=35.56;
else if round(year,1)=1995 then smic=36.98;
else if round(year,1)=1996 then smic=37.91;
else if round(year,1)=1997 then smic=39.43;
else if round(year,1)=1998 then smic=40.22;
else if round(year,1)=1999 then smic=40.72;
else if round(year,1)=2000 then smic=42.02/6.55957;
else if round(year,1)=2001 then smic=43.72/6.55957;
else if round(year,1)=2002 then smic=6.83;
else if round(year,1)=2003 then smic=7.19;
else if round(year,1)=2004 then smic=7.61;
else if round(year,1)=2005 then smic=8.03;
else if round(year,1)=2006 then smic=8.27;
else if round(year,1)=2007 then smic=8.44;
else if round(year,1)=2008 then smic=8.63;
else if round(year,1)=2009 then smic=8.82;
else if round(year,1)=2010 then smic=8.86;
else if round(year,1)=2011 then smic=9.19;
else if round(year,1)=2012 then smic=9.40;
else if round(year,1)=2013 then smic=9.43;
else if round(year,1)=2014 then smic=9.53;
else if round(year,1)=2015 then smic=9.61;
else if round(year,1)=2016 then smic=9.67;
else if round(year,1)=2017 then smic=9.88;
else if round(year,1)=2018 then smic=9.88;
else if round(year,1)=2019 then smic=10.03;
else if round(year,1)=2020 then smic=10.15;
else if round(year,1)=2021 then smic=10.25;

threshold=0.5*smic*1600; * Redefine selection threshold according to your data / 1600 => in France, number of hours a year at 35 hours ;
included=(threshold<=S_BRUT<=smic*1600*1000 and DUREE>359 );
if included=1 then lnwage=log(s_brut);

random=ranuni(15467);

*3 OCCUPATIONS DEFINITION;
Occ1=("21"<=CS<"40")*1;
Occ2=("40"<=CS<"50")*1;
Occ3=("50"<=CS<"70")*1;

if occ1=1 then occ_g=1;
else if occ2=1 then occ_g=2;
else occ_g=3;

*AGE GROUP DEFINITION;
age1=(AGE<=30);
age2=(31<AGE<=40);
age3=(41<AGE<=55);
age4=(55<AGE);

if age1=1 then age_g=1;
else if age2=1 then age_g=2;
else if age3=1 then age_g=3;
else if age4=1 then age_g=4;

*GENDER DEFINITION;
female=(sexe=2);
male=1-female;

*MIGRANT ORIGIN DEFINITION;
if MISSING(DEP_NAISS)=0 then migrant=(DEP_NAISS="99")*1;
nonmigrant=1-migrant;

*SECTORS DEFINITION;
length sector $5.;
if year<2008 then sector=PUT(APET,$naf03_naf08_.); 
else sector=APET; 

if year<2008 then sector_agg=PUT(substr(PUT(sector,$naf03_naf08_.),1,2), $ind08_sectagg.); 
else sector_agg=PUT(substr(sector,1,2), $ind08_sectagg.); 


fin_service=(substr(sector,1,2) in ("64","65","66")); 
manufacturing=("05"<=substr(sector,1,2) <="39"); 


*REGIONAL VARIABLES DEFINITION;
financial_center=(REGT="11");
nuts1=put(substr(COMT,1,2),$ZEAT.);
nuts3=substr(COMT,1,2);


*RANDOM VARIABLE (For sorting);
Random=RANUNI(124567);

*Inclusion/exclusion;
if 0<S_BRUT<300000000;

* Agriculture trees & wood exploitation exclusion (but not fisheries, etc.);
* THIS is partly due to time inconsistencies in French data on agricultural firms (specific social security);
if substr(APET,1,2) not in ("","00","01","02");

rename siren=firm siret=est S_BRUT=wage comt=wtown comr=ltown;

DROP NIC NBHEUR DUREE CPFD SMIC DEP_NAISS 
	 REGT CS SEXE ETRANGER age /*education*/ 
	 APET threshold 
	; 
%mend;


%macro b_a(year);
%let year2=%substr(&year,3,2);
	%macro b_reg(reg);
		DATA b&reg._&year2; 
			SET po&year..post&reg.&year2 
				(RENAME=(ETRANG=ETRANGER DEPNAI=DEP_NAISS REG=REGT CS2=CS COMR=COMR_ BRUT=S_BRUT CIPDZ=CPFD) 
					KEEP=COMR COM DEPR DEP BRUT SIREN DEPNAI CIPDZ APET 
						AGE SEXE ETRANG CS2 NIC REG NBHEUR DUREE);
			ident_s=_N_*100+REGT;
		RUN;
	%mend;
%b_reg(reg=11);
%b_reg(reg=21);
%b_reg(reg=22);
%b_reg(reg=23);
%b_reg(reg=24);
%b_reg(reg=25);
%b_reg(reg=26);
%b_reg(reg=31);
%b_reg(reg=41);
%b_reg(reg=42);
%b_reg(reg=43);
%b_reg(reg=52);
%b_reg(reg=53);
%b_reg(reg=54);
%b_reg(reg=72);
%b_reg(reg=73);
%b_reg(reg=74);
%b_reg(reg=82);
%b_reg(reg=83);
%b_reg(reg=91);
%b_reg(reg=93);
%b_reg(reg=94);

DATA b&year; 
set 
b11_&year2 (WHERE=(REGT="11" ))
b21_&year2 (WHERE=(REGT="21" ))
b22_&year2 (WHERE=(REGT="22" ))
b23_&year2 (WHERE=(REGT="23" ))
b24_&year2 (WHERE=(REGT="24" ))
b25_&year2 (WHERE=(REGT="25" ))
b26_&year2 (WHERE=(REGT="26" ))
b31_&year2 (WHERE=(REGT="31" ))
b41_&year2 (WHERE=(REGT="41" ))
b42_&year2 (WHERE=(REGT="42" ))
b43_&year2 (WHERE=(REGT="43" ))
b52_&year2 (WHERE=(REGT="52" ))
b53_&year2 (WHERE=(REGT="53" ))
b54_&year2 (WHERE=(REGT="54" ))
b72_&year2 (WHERE=(REGT="72" ))
b73_&year2 (WHERE=(REGT="73" ))
b74_&year2 (WHERE=(REGT="74" ))
b82_&year2 (WHERE=(REGT="82" ))
b83_&year2 (WHERE=(REGT="83" ))
b91_&year2 (WHERE=(REGT="91" ))
b93_&year2 (WHERE=(REGT="93" ))
b94_&year2 (WHERE=(REGT="94" ))
;
COMR=DEPR!!COMR_;
COMT=DEP!!COM;
year=&year;
%common;
drop COMR_ COM DEPR DEP;
run;

PROC DATASETS library=user;
	DELETE 
	b11_&year2
	b21_&year2
	b22_&year2
	b23_&year2
	b24_&year2
	b25_&year2
	b26_&year2
	b31_&year2
	b41_&year2
	b42_&year2
	b43_&year2
	b52_&year2
	b53_&year2
	b54_&year2
	b72_&year2
	b73_&year2
	b74_&year2
	b82_&year2
	b83_&year2
	b91_&year2
	b93_&year2
	b94_&year2;
RUN;
%mend;


%macro b_a_1(year);
%let year2=%substr(&year,3,2);
	%macro b_reg(reg);
		DATA b&reg._&year2; 
			SET po&year..post&reg.&year2 
				(RENAME=(ETRANG=ETRANGER DEPNAI=DEP_NAISS REG=REGT CS_1=CS BRUT_1=S_BRUT CIPDZ_1=CPFD NBHEUR_1=NBHEUR DUREE_1=DUREE) 
					keep=COMR_1 COM DEPR_1 DEP BRUT_1 SIREN DEPNAI CIPDZ_1 APET AGE SEXE ETRANG CS_1 NIC REG NBHEUR_1 DUREE_1);
			ident_s=_N_*100+REGT;
		RUN;
	%mend;
%b_reg(reg=11);
%b_reg(reg=21);
%b_reg(reg=22);
%b_reg(reg=23);
%b_reg(reg=24);
%b_reg(reg=25);
%b_reg(reg=26);
%b_reg(reg=31);
%b_reg(reg=41);
%b_reg(reg=42);
%b_reg(reg=43);
%b_reg(reg=52);
%b_reg(reg=53);
%b_reg(reg=54);
%b_reg(reg=72);
%b_reg(reg=73);
%b_reg(reg=74);
%b_reg(reg=82);
%b_reg(reg=83);
%b_reg(reg=91);
%b_reg(reg=93);
%b_reg(reg=94);

DATA b&year._1; 
set 
b11_&year2 (WHERE=(REGT="11" ))
b21_&year2 (WHERE=(REGT="21" ))
b22_&year2 (WHERE=(REGT="22" ))
b23_&year2 (WHERE=(REGT="23" ))
b24_&year2 (WHERE=(REGT="24" ))
b25_&year2 (WHERE=(REGT="25" ))
b26_&year2 (WHERE=(REGT="26" ))
b31_&year2 (WHERE=(REGT="31" ))
b41_&year2 (WHERE=(REGT="41" ))
b42_&year2 (WHERE=(REGT="42" ))
b43_&year2 (WHERE=(REGT="43" ))
b52_&year2 (WHERE=(REGT="52" ))
b53_&year2 (WHERE=(REGT="53" ))
b54_&year2 (WHERE=(REGT="54" ))
b72_&year2 (WHERE=(REGT="72" ))
b73_&year2 (WHERE=(REGT="73" ))
b74_&year2 (WHERE=(REGT="74" ))
b82_&year2 (WHERE=(REGT="82" ))
b83_&year2 (WHERE=(REGT="83" ))
b91_&year2 (WHERE=(REGT="91" ))
b93_&year2 (WHERE=(REGT="93" ))
b94_&year2 (WHERE=(REGT="94" ))
;
COMR=DEPR_1!!COMR_1;
COMT=DEP!!COM;
year=&year-0.9;
age=age-1;
CS=SUBSTR(CS,1,2);
%common;
drop COMR_1 COM DEPR_1 DEP;
run;

PROC DATASETS library=user;
	DELETE 
	b11_&year2
	b21_&year2
	b22_&year2
	b23_&year2
	b24_&year2
	b25_&year2
	b26_&year2
	b31_&year2
	b41_&year2
	b42_&year2
	b43_&year2
	b52_&year2
	b53_&year2
	b54_&year2
	b72_&year2
	b73_&year2
	b74_&year2
	b82_&year2
	b83_&year2
	b91_&year2
	b93_&year2
	b94_&year2;
RUN;
%mend;

%macro b_b(year);
%let keep=COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE ETRANGER CS NIC REGT NBHEUR DUREE;
%let year2=%substr(&year,3,2);
%let where=TYP_EMPLOI NOT IN ("E","I");
data b&year; 
set 
po&year..post11&year2 (KEEP=&keep WHERE=(REGT="11" AND &where )) 
po&year..post21&year2 (KEEP=&keep WHERE=(REGT="21" AND &where )) 
po&year..post22&year2 (KEEP=&keep WHERE=(REGT="22" AND &where )) 
po&year..post23&year2 (KEEP=&keep WHERE=(REGT="23" AND &where )) 
po&year..post24&year2 (KEEP=&keep WHERE=(REGT="24" AND &where )) 
po&year..post25&year2 (KEEP=&keep WHERE=(REGT="25" AND &where )) 
po&year..post26&year2 (KEEP=&keep WHERE=(REGT="26" AND &where )) 
po&year..post31&year2 (KEEP=&keep WHERE=(REGT="31" AND &where )) 
po&year..post41&year2 (KEEP=&keep WHERE=(REGT="41" AND &where )) 
po&year..post42&year2 (KEEP=&keep WHERE=(REGT="42" AND &where )) 
po&year..post43&year2 (KEEP=&keep WHERE=(REGT="43" AND &where )) 
po&year..post52&year2 (KEEP=&keep WHERE=(REGT="52" AND &where )) 
po&year..post53&year2 (KEEP=&keep WHERE=(REGT="53" AND &where )) 
po&year..post54&year2 (KEEP=&keep WHERE=(REGT="54" AND &where )) 
po&year..post72&year2 (KEEP=&keep WHERE=(REGT="72" AND &where )) 
po&year..post73&year2 (KEEP=&keep WHERE=(REGT="73" AND &where )) 
po&year..post74&year2 (KEEP=&keep WHERE=(REGT="74" AND &where )) 
po&year..post82&year2 (KEEP=&keep WHERE=(REGT="82" AND &where )) 
po&year..post83&year2 (KEEP=&keep WHERE=(REGT="83" AND &where )) 
po&year..post91&year2 (KEEP=&keep WHERE=(REGT="91" AND &where )) 
po&year..post93&year2 (KEEP=&keep WHERE=(REGT="93" AND &where ))
po&year..post94&year2 (KEEP=&keep WHERE=(REGT="94" AND &where )) 
;
year=&year;
%common;
drop TYP_EMPLOI;
run;
%mend;


%macro b_b_1(year);
%let year2=%substr(&year,3,2);
%let rename= CS_1=CS APET_1=APET COMR_1=COMR COMT_1=COMT ETRANGER_1=ETRANGER S_BRUT_1=S_BRUT CPFD_1=CPFD REGT_1=REGT NBHEUR_1=NBHEUR DUREE_1=DUREE;
%let keep= COMR_1 COMT_1 S_BRUT_1 SIREN IDENT_S DEP_NAISS CPFD_1 APET_1 TYP_EMPLOI_1 AGE SEXE ETRANGER_1 CS_1 NIC REGT_1 NBHEUR_1 DUREE_1;
data b&year._1; 
set 
po&year..post11&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="11" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post21&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="21" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post22&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="22" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post23&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="23" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post24&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="24" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post25&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="25" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post26&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="26" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post31&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="31" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post41&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="41" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post42&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="42" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post43&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="43" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post52&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="52" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post53&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="53" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post54&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="54" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post72&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="72" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post73&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="73" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post74&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="74" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post82&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="82" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post83&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="83" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post91&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="91" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
po&year..post93&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="93" AND TYP_EMPLOI_1 NOT IN ("E","I") ))
po&year..post94&year2 (RENAME=(&rename )KEEP=&keep  WHERE=(REGT="94" AND TYP_EMPLOI_1 NOT IN ("E","I") )) 
;
year=&year-0.9;
age=age-1;
%common;
drop TYP_EMPLOI_1;
run;
%mend;



%macro b_c(year);
%let keep= COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE IND_ETRANGER PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE;
%let rename= PCS=CS IND_ETRANGER=ETRANGER;
%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I");
data b&year; 
set 
po&year..post11aa (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where )) 
po&year..post11bb (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where )) 
po&year..post11cc (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where )) 
po&year..post11dd (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where )) 
po&year..post21 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="21" AND &where )) 
po&year..post22 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="22" AND &where )) 
po&year..post23 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="23" AND &where )) 
po&year..post24 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="24" AND &where )) 
po&year..post25 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="25" AND &where )) 
po&year..post26 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="26" AND &where )) 
po&year..post31 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="31" AND &where )) 
po&year..post41 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="41" AND &where )) 
po&year..post42 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="42" AND &where )) 
po&year..post43 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="43" AND &where )) 
po&year..post52 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="52" AND &where )) 
po&year..post53 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="53" AND &where )) 
po&year..post54 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="54" AND &where )) 
po&year..post72 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="72" AND &where )) 
po&year..post73 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="73" AND &where )) 
po&year..post74 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="74" AND &where )) 
po&year..post82 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="82" AND &where )) 
po&year..post83 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="83" AND &where )) 
po&year..post91 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="91" AND &where )) 
po&year..post93 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="93" AND &where ))
po&year..post94 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="94" AND &where )) 
;
year=&year;
CS=SUBSTR(CS,1,2);
%common;
drop TYP_EMPLOI DOMEMPL_EMPL;
run;
%mend;



%macro b_d(year);
%let keep=COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE IND_ETRANGER PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE;
%let rename= PCS=CS IND_ETRANGER=ETRANGER;
%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I");
data b&year; 
set 
po&year..post11aa (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where ))
po&year..post11bb (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where ))
po&year..post11cc (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where ))
po&year..post11dd (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="11" AND &where ))
po&year..post21 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="21" AND &where )) 
po&year..post22 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="22" AND &where )) 
po&year..post23 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="23" AND &where )) 
po&year..post24 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="24" AND &where )) 
po&year..post25 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="25" AND &where )) 
po&year..post26 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="26" AND &where )) 
po&year..post31 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="31" AND &where )) 
po&year..post41 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="41" AND &where )) 
po&year..post42 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="42" AND &where )) 
po&year..post43 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="43" AND &where )) 
po&year..post52 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="52" AND &where )) 
po&year..post53 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="53" AND &where )) 
po&year..post54 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="54" AND &where )) 
po&year..post72 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="72" AND &where )) 
po&year..post73 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="73" AND &where )) 
po&year..post74 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="74" AND &where )) 
po&year..post82a (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="82" AND &where )) 
po&year..post82b (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="82" AND &where )) 
po&year..post83 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="83" AND &where )) 
po&year..post91 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="91" AND &where )) 
po&year..post93 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="93" AND &where )) 
po&year..post94 (RENAME=(&rename) KEEP=&keep  WHERE=(REGT="94" AND &where )) 
;
year=&year;
CS=SUBSTR(CS,1,2);
%common;
drop TYP_EMPLOI DOMEMPL_EMPL;
run;
%mend;


%macro b_e(year);
%let keep=COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE IND_ETRANGER PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE ;
%let rename= PCS=CS IND_ETRANGER=ETRANGER;
%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I");
data b&year; 
set 
po&year..post11 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="11" AND &where ))
po&year..post22 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="22" AND &where )) 
po&year..post23 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="23" AND &where )) 
po&year..post24 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="24" AND &where )) 
po&year..post25 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="25" AND &where )) 
po&year..post26 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="26" AND &where )) 
po&year..post31 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="31" AND &where )) 
po&year..post41 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="41" AND &where )) 
po&year..post42 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="42" AND &where )) 
po&year..post43 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="43" AND &where )) 
po&year..post52 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="52" AND &where )) 
po&year..post53 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="53" AND &where )) 
po&year..post54 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="54" AND &where )) 
po&year..post72 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="72" AND &where )) 
po&year..post73 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="73" AND &where )) 
po&year..post74 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="74" AND &where )) 
po&year..post82 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="82" AND &where )) 
po&year..post83 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="83" AND &where )) 
po&year..post91 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="91" AND &where )) 
po&year..post93 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="93" AND &where )) 
po&year..post94 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="94" AND &where )) 
;
year=&year;
CS=SUBSTR(CS,1,2);
%common;
drop TYP_EMPLOI DOMEMPL_EMPL;
run;
%mend;

%macro b_f(year);
	%let keep=COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE IND_ETRANGER PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE DEPT;
	%let rename= PCS=CS IND_ETRANGER=ETRANGER;
	%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I");
	data b&year; 
		set 
		pi&year..post75 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="75" AND &where ))
		pi&year..post77 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="77" AND &where ))
		pi&year..post78 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="78" AND &where ))
		pi&year..post91 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="91" AND &where ))
		pi&year..post92 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="92" AND &where ))
		pi&year..post93 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="93" AND &where ))
		pi&year..post94 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="94" AND &where ))
		pi&year..post95 (RENAME=(&rename) KEEP=&keep WHERE=(DEPT="95" AND &where ))

		po&year..post22 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="22" AND &where )) 
		po&year..post23 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="23" AND &where )) 
		po&year..post24 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="24" AND &where )) 
		po&year..post25 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="25" AND &where )) 
		po&year..post26 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="26" AND &where )) 
		po&year..post31 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="31" AND &where )) 
		po&year..post41 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="41" AND &where )) 
		po&year..post42 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="42" AND &where )) 
		po&year..post43 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="43" AND &where )) 
		po&year..post52 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="52" AND &where )) 
		po&year..post53 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="53" AND &where )) 
		po&year..post54 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="54" AND &where )) 
		po&year..post72 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="72" AND &where )) 
		po&year..post73 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="73" AND &where )) 
		po&year..post74 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="74" AND &where )) 
		po&year..post82 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="82" AND &where )) 
		po&year..post83 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="83" AND &where )) 
		po&year..post91 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="91" AND &where )) 
		po&year..post93 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="93" AND &where )) 
		po&year..post94 (RENAME=(&rename) KEEP=&keep WHERE=(REGT="94" AND &where )) 
		;
		year=&year;
		CS=SUBSTR(CS,1,2);
		%common;
		drop TYP_EMPLOI DOMEMPL_EMPL DEPT;
	run;
%mend;

%macro b_g(year);
	%if &year<2016 %then %do; 
		%let IND_ETRANGER= IND_ETRANGER;
		%let rename= RENAME=(IND_ETRANGER=ETRANGER PCS=CS) ; 
	%end;
	%else %do; 
		%let IND_ETRANGER= ;
		%let rename= RENAME=(PCS=CS) ; 
	%end;
	%let keep= COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE &IND_ETRANGER PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE DEPT  ;
	%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I");
	
	data b&year; 
		set 
		pi&year..post75 (&rename KEEP=&keep where=(DEPT="75" AND &where))
		pi&year..post77 (&rename KEEP=&keep where=(DEPT="77" AND &where))
		pi&year..post78 (&rename KEEP=&keep where=(DEPT="78" AND &where))
		pi&year..post91 (&rename KEEP=&keep where=(DEPT="91" AND &where))
		pi&year..post92 (&rename KEEP=&keep where=(DEPT="92" AND &where))
		pi&year..post93 (&rename KEEP=&keep where=(DEPT="93" AND &where))
		pi&year..post94 (&rename KEEP=&keep where=(DEPT="94" AND &where))
		pi&year..post95 (&rename KEEP=&keep where=(DEPT="95" AND &where))

		po&year..post24 (&rename KEEP=&keep WHERE=(REGT="24" AND &where)) 
		po&year..post27 (&rename KEEP=&keep WHERE=(REGT="27" AND &where)) 
		po&year..post28 (&rename KEEP=&keep WHERE=(REGT="28" AND &where)) 
		po&year..post32 (&rename KEEP=&keep WHERE=(REGT="32" AND &where)) 
		po&year..post44 (&rename KEEP=&keep WHERE=(REGT="44" AND &where)) 
		po&year..post52 (&rename KEEP=&keep WHERE=(REGT="52" AND &where)) 
		po&year..post53 (&rename KEEP=&keep WHERE=(REGT="53" AND &where)) 
		po&year..post75 (&rename KEEP=&keep WHERE=(REGT="75" AND &where)) 
		po&year..post76 (&rename KEEP=&keep WHERE=(REGT="76" AND &where)) 
		po&year..post84 (&rename KEEP=&keep WHERE=(REGT="84" AND &where)) 
		po&year..post93 (&rename KEEP=&keep WHERE=(REGT="93" AND &where))
		po&year..post94 (&rename KEEP=&keep WHERE=(REGT="94" AND &where))

		;
			year=&year;
		%common;		 
		drop TYP_EMPLOI DOMEMPL_EMPL;
	run;
%mend;

%macro b_h(year);
	%let keep= 	COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE /*IND_ETRANGER*/ PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE DEPT  ;
	%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I")  AND DEPT NOT IN ("97","98","99") and substr(COMT,1,2) NOT IN ("97","98","99") ;
	%let rename=RENAME=(PCS=CS) ; 

	data b&year; 
		set 
		po&year..post_1 (&rename KEEP=&keep WHERE=(&where))
		po&year..post_2 (&rename KEEP=&keep WHERE=(&where))
		po&year..post_3 (&rename KEEP=&keep WHERE=(&where))
		po&year..post_4 (&rename KEEP=&keep WHERE=(&where))
		;
		year=&year;
			%common;		 
		CS=SUBSTR(CS,1,2);
		drop TYP_EMPLOI DOMEMPL_EMPL;
	run;
%mend;



%macro b_i(year);
	%let keep= 	COMR COMT S_BRUT SIREN IDENT_S DEP_NAISS CPFD APET TYP_EMPLOI AGE SEXE /*IND_ETRANGER*/ PCS NIC REGT DOMEMPL_EMPL NBHEUR DUREE DEPT  ;
	%let where=DOMEMPL_EMPL not in ("1","7") AND TYP_EMPLOI NOT IN ("E","I")  AND DEPT NOT IN ("97","98","99") and substr(COMT,1,2) NOT IN ("97","98","99") ;
	%let rename=RENAME=(PCS=CS) ; 

	data b&year; 
		set 
		po&year..post (&rename KEEP=&keep WHERE=(&where))
		;
		year=&year;
			%common;		 
		CS=SUBSTR(CS,1,2);
		drop TYP_EMPLOI DOMEMPL_EMPL;
	run;
%mend;

%b_a_1(1994);%seg2(b1994_1);
%b_a(1994);%seg2(b1994);
%b_a(1995);%seg2(b1995);
%b_a(1996);%seg2(b1996);
%b_a(1997);%seg2(b1997);
%b_a(1998);%seg2(b1998);
%b_a(1999);%seg2(b1999);
%b_a(2000);%seg2(b2000);
%b_a(2001);%seg2(b2001);
%b_b_1(2002);%seg2(b2002_1);
%b_b(2002);%seg2(b2002);
%b_b(2003);%seg2(b2003);
%b_b(2004);%seg2(b2004);
%b_b(2005);%seg2(b2005);
%b_b(2006);%seg2(b2006);
%b_b(2007);%seg2(b2007);
%b_b(2008);%seg2(b2008);
/*%b_c_1(2009);%seg2(b2009_1);*/
%b_c(2009);%seg2(b2009);
%b_d(2010);%seg2(b2010);
%b_e(2011);%seg2(b2011);
%b_f(2012);%seg2(b2012);
%b_f(2013);%seg2(b2013);
%b_g(2014);%seg2(b2014);
%b_g(2015);%seg2(b2015);
%b_g(2016);%seg2(b2016);
%b_g(2017);%seg2(b2017);
%b_h(2018);%seg2(b2018);
%b_h(2019);%seg2(b2019);
%b_i(2020);%seg2(b2020);


%macro collect(key,description);
data &key; 
set 
REC.B1994_1&key
REC.B1994&key
REC.B1995&key
REC.B1996&key
REC.B1997&key
REC.B1998&key
REC.B1999&key
REC.B2000&key
REC.B2001&key
REC.B2002_1&key
REC.B2002&key
REC.B2003&key
REC.B2004&key
REC.B2005&key
REC.B2006&key
REC.B2007&key
REC.B2008&key
/*REC.B2009_1&key*/
REC.B2009&key
REC.B2010&key
REC.B2011&key
REC.B2012&key
REC.B2013&key
REC.B2014&key
REC.B2015&key
REC.B2016&key
REC.B2017&key
REC.B2018&key
REC.B2019&key
; 
length file $10.;
file="&key";
length description $100.;
description="&description";
run;
%mend;

%macro mcollect(key,des2);
%collect(outest&key , &des2 in establishments);
*%collect(outw_firm&key , &des2 in establishments within firms);
*%collect(outwwtown&key , &des2 in establishments within working town);
*%collect(outr&key , &des2 in reshuffled establishments);
%collect(outfirm&key , &des2 in firms);
*%collect(outwtown&key , &des2 in working town);
%collect(outltown&key , &des2 in living town);

data rec.&key; set
outest&key 
/*outw_firm&key */
/*outw_wtown&key
outr&key */
outfirm&key 
/*outwtown&key */
outltown&key ;

if _stat_="N" then N_wage=wage;
if _stat_="MAX" then MAX_wage=wage;
if _stat_="MEAN" then MEAN_wage=wage;
retain N_wage MAX_WAGE MEAN_WAGE;

if _stat_="MEAN" then Ctrl_N=N_WAGE;
if _stat_="MEAN" then Ctrl_MasseSal=N_WAGE*MEAN_Wage;
if _stat_="MEAN" then Ctrl_MaxMasseSal=MAX_WAGE/(N_WAGE*MEAN_Wage);

DROP N_wage MEAN_wage MAX_wage;
run;
proc means data=rec.&key MEAN MIN MAX ; 
title "Control statistical secret for" &key..txt;
var Ctrl_N Ctrl_MaxMasseSal;
run;

%mend;

%mcollect(inc,Exposure income classes);
*%mcollect(inc2,Discrimination income classes);
%mcollect(dinc,Exposure income deciles);

%mcollect(sex,Exposure Gender);
*%mcollect(sex2,Discrimination Gender);
*%mcollect(for,Exposure Foreigner);
*%mcollect(for2,Discrimination Foreigner);
%mcollect(mig,Exposure For_born);
*%mcollect(mig2,Discrimination For_born);
%mcollect(age,Exposure Age);
*%mcollect(age2,Discrimination Age);
%mcollect(occ,Exposure Occupation);
*%mcollect(occ2,Discrimination Occupation);

*Descriptives; 
%collect(estdes, Descriptives on establishement);
%collect(firmdes, Descriptives on firms);
*%collect(wtowndes, Descriptives on town of work);
%collect(ltowndes, Descriptives on town of living);

data rec.des; set
estdes
firmdes
/*wtowndes*/
ltowndes;

if _stat_="N" then N_wage=wage;
if _stat_="MAX" then MAX_wage=wage;
if _stat_="MEAN" then MEAN_wage=wage;
retain N_wage MAX_WAGE MEAN_WAGE;

if _stat_="MEAN" then Ctrl_N=N_WAGE;
if _stat_="MEAN" then Ctrl_MasseSal=N_WAGE*MEAN_Wage;
if _stat_="MEAN" then Ctrl_MaxMasseSal=MAX_WAGE/(N_WAGE*MEAN_Wage);

DROP N_wage MEAN_wage MAX_wage;
run;

proc means data=rec.des MEAN MIN MAX ; var Ctrl_N Ctrl_MaxMasseSal;
title "Control statistical secret for des.txt";
run;

run;

/**Regressions; 
%collect(outreg, Regressions);
data rec.reg; set outreg; run;*/



proc gplot data=rec.inc;
title "N per wage groups";
symbol i=line value=circle h=0.5;
plot wage*year=rk;
where year>=1993 and rk NE . and _STAT_="N" and file="outestinc" and _type_=64; 
run;


proc gplot data=rec.inc;
title "Mean wage per wage groups";
symbol i=line value=circle h=0.5;
plot wage*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outestinc" and _type_=64; 
run;

proc gplot data=rec.inc;
title "Exposure in establishments per wage groups ";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outestinc" and _type_=64; 
run;


proc gplot data=rec.inc;
title "Exposure in firms per wage groups ";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outfirminc" and _type_=1; 
run;

/*
proc gplot data=rec.inc;
title "Exposure in working town per wage groups ";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outwtownin" and _type_=2; 
run;
*/

proc gplot data=rec.inc;
title "Exposure in living town per wage groups ";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outltownin" and _type_=1; 
run;


proc gplot data=rec.inc;
title "Exposure in financial services";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outestinc" and fin_service=1 ;
; 
run;


proc gplot data=rec.inc;
title "Exposure outside financial services";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outestinc" and fin_service=0 ;
; 
run;


proc gplot data=rec.inc;
title "Exposure inside manufacture";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outestinc" and manufacturing=1 ;
; 
run;


proc gplot data=rec.inc;
title "Exposure in global city";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outestinc" and financial_center=1 ;
; 
run;


proc gplot data=rec.inc;
title "Exposure in the provinces";
symbol i=line value=circle h=0.5;
plot xF0025*year=rk;
plot xF2575*year=rk;
plot xF7590*year=rk;
plot xF9099*year=rk;
plot xF9910*year=rk;
where year>=1993 and rk NE . and _STAT_="MEAN" and file="outestinc" and financial_center=0 ;
; 
run;


%macro export(folder,file);
PROC EXPORT DATA= REC.&file 
            OUTFILE= "&folder.&file._CTRL.txt" 
            DBMS=TAB REPLACE;
     PUTNAMES=YES;
RUN;

data temp; set REC.&file (drop=Ctrl_N Ctrl_MasseSal Ctrl_MaxMasseSal); 
run;

PROC EXPORT DATA= temp
            OUTFILE= "&folder.&file..txt" 
            DBMS=TAB REPLACE;
     PUTNAMES=YES;
RUN;

%mend;

/*proc contents data=REC.AGE;run;*/

%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,AGE);
%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,INC);
%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,DINC);
%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,OCC);
%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,SEX);
%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,MIG);
%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,DES);
*%export(C:\Users\Public\Documents\Export\Export_2021_11_04\Segregation\,REG);

*%export(P:\Export\Export_2019_10_22\Segregation\,FOR);
